﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Resource;
using NFlickr.Communication;
using NFlickr.Core.Interface;
using NFlickr.Factory;

namespace NFlickr.Core
{
    public class Photo : RequireSignedServiceBase
    {
        public  Photo(FlickrService service)
        {
            Token = service.Token;
        }

        public Token Token
        {
            get;
            internal set;
        }

        public string Title
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }
        public IList<string> Tags
        {
            get;
            set;
        }

        public bool Public
        {
            get;
            set;
        }

        public bool Friend
        {
            get;
            set;
        }

        public bool Family
        {
            get;
            set;
        }

        public SafetyLevel SafetyLevel
        {
            get;
            set;
        }

        public ContentType ContentType
        {
            get;
            set;
        }


        private int _hidden;
        public bool Hidden
        {
            get
            {
                return _hidden == 1 ? false : true;
            }
            set
            {
                _hidden = value ? 2 : 1;
            }
        }


        public override string MethodName
        {
            get { return string.Empty; }
        }

        public override string TagName
        {
            get { return "photoid"; }
        }

        public string PhotoId
        {
            get;
            internal set;
        }

        public override bool Refresh()
        {
            throw new NotImplementedException();
        }

        public string Upload(string fileName)
        {
            IChannel channel = FlickrChannelFactory.CreateInstance();
            channel.Complete +=new EventHandler<SendRequestCompletedEventArgs>(channel_Complete);
            channel.SignatureRequired += new NFlickr.Core.Interface.SignatureRequiredEventHandler(channel_SignatureRequired);


            Params = new Dictionary<string, string>();

            Params.Add(NFlickr.Resource.FlickrService.ApplicationKeyName, ApplicationKey);


            if (!string.IsNullOrEmpty(Title))
            {
                Params.Add("title", Title);
            }
            if (!string.IsNullOrEmpty(Description))
            {
                Params.Add("description", Description);
            }
            if (Tags.Count > 0)
            {
                string tagValue = string.Empty;
                foreach (string tag in Tags)
                {
                    tagValue += tag + " ";
                }
                Params.Add("tags", tagValue);
            }
            string publicValue = Public ? "1" : "0";
            Params.Add("is_public", publicValue);

            string friendValue = Friend ? "1" : "0";
            Params.Add("is_friend", friendValue);

            string familyValue = Family ? "1" : "0";
            Params.Add("is_family", familyValue);

            if (SafetyLevel != SafetyLevel.None)
            {
                Params.Add("safety_level", ((int)SafetyLevel).ToString());
            }
            if (ContentType != ContentType.None)
            {
                Params.Add("content_type", ((int)ContentType).ToString());
            }
            if (Hidden)
            {
                Params.Add("hidden", _hidden.ToString());
            }

            Params.Add("auth_token", Token.Value);

            IResult result = channel.UploadFile(NFlickr.Resource.Url.UploadUrl, this, "photo", fileName, "image/jpeg");

            if (result != null && result.ResultCode == 0)
            {
                return result.ResultString;
            }
            return string.Empty;
        }

        string channel_SignatureRequired(object sender, NFlickr.Core.Interface.SignatureRequiredEventArgs args)
        {
            Params.Add(NFlickr.Resource.FlickrService.SignatureKeyName, args.Singnature);
            return GetQueryString(Params);
        }

        void channel_Complete(object sender, NFlickr.Core.Interface.SendRequestCompletedEventArgs args)
        {

            args.Result = ParseResult(args.Response);
        }
    }
}
